package com.bawei.module_home.ui.news

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.alibaba.android.arouter.utils.TextUtils
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.module_home.R
import com.bawei.module_home.adapter.NewsAdapter
import com.bawei.module_home.databinding.ActivityNewsBinding
import com.bawei.module_home.http.HomeServiceViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.NEWS_PAGE)
class NewsActivity : BaseActivity<HomeServiceViewModel, ActivityNewsBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_news
    private val mViewModel: HomeServiceViewModel by viewModels()
    private val mAdapter: NewsAdapter by lazy {
        NewsAdapter()
    }
    override fun initView() {
        super.initView()
        initAdapter()
        mBinding.toobar.apply {
            title.text="园区新闻"
            more.setImageResource(R.drawable.add)
            more.setOnClickListener({
                ARouter.getInstance().build(Router.NEWS_ADD_PAGE).navigation()
            })
            back.setOnClickListener({
                finish()
            })

        }

        mBinding.search.setOnQueryTextListener(object : androidx.appcompat.widget.SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String): Boolean {
                searchAreaSpaces(query)
                return true
            }

            override fun onQueryTextChange(newText: String): Boolean {
                searchAreaSpaces(newText)
                return true
            }

        })
    }

    override fun initData() {
        mViewModel.getNews()
    }

    override fun initListener() {
        mViewModel.newsState.observeWithLifecycle(this){
           when(it){
               Empty -> {}
               is Failed -> {}
               Loading -> {}
               Success ->{
                   mAdapter.setData(mViewModel.newsList)
               }
           }
        }
    }

    fun initAdapter(){
        mBinding.rv.layoutManager= LinearLayoutManager(this)
        mBinding.rv.adapter=mAdapter
    }

    fun searchAreaSpaces(query: String){
        if(TextUtils.isEmpty(query)){
            mAdapter.setData(mViewModel.newsList)
        }else{
            mViewModel.newsNewList.clear()
            var lowerCaseQuery=query.toLowerCase()
            for (modelItem in mViewModel.newsList) {
                if(modelItem.title.contains(lowerCaseQuery)){
                    mViewModel.newsNewList.add(modelItem)
                }
            }
            mAdapter.setData(mViewModel.newsNewList)
        }
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getNews()
    }
}